package logic

import (
	"context"
	"xialou_platform/app/errorx"
	"xialou_platform/services/agent/model"

	"xialou_platform/services/agent/rpc/agentClient"
	"xialou_platform/services/agent/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type StoreLogLatestByStoreIdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewStoreLogLatestByStoreIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StoreLogLatestByStoreIdLogic {
	return &StoreLogLatestByStoreIdLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商家最新的日志
func (l *StoreLogLatestByStoreIdLogic) StoreLogLatestByStoreId(in *agentClient.IdBody) (*agentClient.StoreLog, error) {
	storeLog, err := l.svcCtx.StoreLogModel.FindLatestByStoreId(l.ctx, in.Id)
	if err != nil {
		if err == model.ErrNotFound {
			logx.Infof("商家最新的日志 找不到该商户。(%d)", in.Id)
			return nil, errorx.NewDefaultError("找不到该商户。")
		}
		return nil, err
	}

	return &agentClient.StoreLog{
		Id:         storeLog.Id,
		StoreId:    storeLog.StoreId,
		Status:     storeLog.Status,
		Body:       storeLog.Body,
		Operation:  storeLog.Operation,
		CreateTime: storeLog.CreateTime.Unix(),
		UpdateTime: storeLog.UpdateTime.Unix(),
		Remark:     storeLog.Remark,
		AgentId:    storeLog.AgentId,
		Reviewer:   storeLog.Reviewer,
	}, nil
}
